package com.wbdgj.ui.booking;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.BalanceAdapter;
import com.wbdgj.adapter.YueAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultBalanceModel;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.home.RechargeActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/6/1.
 */

public class BalanceActivity extends BaseActivity{
    private Context context = this;
    @BindView(R.id.yue)
    TextView yue;
    @BindView(R.id.demo_listview)
    PullListView myPullListView;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    private int page = 1;
    private int totalPage;
    private ArrayList<LinkedTreeMap> linkedTreeMapList;
    private BalanceAdapter yueAdapter;

    private String ID_CARD;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_balance;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void initView() {
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        my_fund_record();
                        hotel_user_info();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

                if ((page*10) > totalPage){
                    ToastUtils.toastShort("已无更多");
                }else{
                    page = page + 1;
                    my_fund_record();
                }
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });
        ID_CARD = getIntent().getStringExtra("ID_CARD");
        hotel_user_info();
        my_fund_record();

        findViewById(R.id.czLai).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               ToastUtils.toastShort("暂不支持App充值！");
            }
        });
    }

    private void hotel_user_info() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().hotel_user_info(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), ID_CARD);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    yue.setText("￥"+linkedTreeMap.get("account_balance")+"");
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void my_fund_record() {
        Call<ResultBalanceModel> call = HttpAdapter.getSerives().my_fund_record(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), ID_CARD, page+"", "10");
        call.enqueue(new OnResponseListener<ResultBalanceModel>(context) {
            @Override
            protected void onSuccess(ResultBalanceModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap pageLinkedTreeMap = (com.google.gson.internal.LinkedTreeMap) userModelBaseResultModel.getPager();
                    totalPage = Integer.parseInt(decimalFormat.format(pageLinkedTreeMap.get("total")));

                    linkedTreeMapList = new ArrayList<>();
                    linkedTreeMapList = (ArrayList<LinkedTreeMap>) userModelBaseResultModel.getData();
                    if (page == 1){
                        yueAdapter = new BalanceAdapter(context, linkedTreeMapList);
                        myPullListView.setAdapter(yueAdapter);
                    }else{
                        yueAdapter.loadMoreListView(linkedTreeMapList);
                        yueAdapter.notifyDataSetChanged();
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
